// ActionScript file
package {
	//从A3D包中导入所需要的类
	import alternativa.engine3d.controllers.*;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.engine3d.materials.WireMaterial;
	import alternativa.engine3d.primitives.Box;
	import alternativa.utils.FPS;
	//从flash 包中导入所需要的类
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	//设置输出地SWF文件的背景色和帧频
	[SWF(backgroundColor="#000000", frameRate="100")]
	
	public class A3dTest extends Sprite	{
		//声明所需要的变量
		private var scene:Scene3D;
		private var view:View;
		private var camera:Camera3D;
		private var cameraController:CameraController;
		private var box:Box;
		
		public function A3dTest()	{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(e:Event): void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			//设置舞台缩放模式，当拉动播放器时，画面保持原始大小比例显示。
			stage.scaleMode = StageScaleMode.NO_SCALE;
			//设置舞台对齐方式，使舞台靠左上角对齐。
			stage.align = StageAlign.TOP_LEFT;
			//创建场景
			scene = new Scene3D();
			scene.root = new Object3D();
			box = new Box(100, 100, 100, 3, 3, 3);
			box.cloneMaterialToAllSurfaces(new WireMaterial(1, 0xFFFFFF));
			scene.root.addChild(box);
			//添加相机和视窗
			camera = new Camera3D();
			camera.x = 100;
			camera.y = -150;
			camera.z = 100;
			scene.root.addChild(camera);
			view = new View();
			addChild(view);
			view.camera = camera;
			//添加相机控制器
			cameraController = new CameraController(stage);
			cameraController.camera = camera;
			cameraController.setDefaultBindings();
			cameraController.checkCollisions = true;
			cameraController.collisionRadius = 20;
			cameraController.lookAt(box.coords);
			cameraController.controlsEnabled = true;
			//添加帧频显示
			FPS.init(stage);
			
			//为stage添加事件侦听
			stage.addEventListener(Event.RESIZE, onResize);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			onResize(null);
		}
		
		private function onResize(e:Event):void {
			view.width = stage.stageWidth;
			view.height = stage.stageHeight;
		}
		
		private function onEnterFrame(e:Event):void {
			//用户输入响应
			cameraController.processInput();
			//场景计算重绘
			scene.calculate();
		}
	}
}
